/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.impl.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;

import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.UnsupportedMediaTypeException;
import com.semagia.atomico.server.storage.StorageException;

/**
 * Resource which provides a fragments entry and a snapshots entry for
 * a collection.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
@Path("/{collection}/")
public final class CollectionFeedResource extends AbstractCollectionAwareFeedResource {

    public CollectionFeedResource(@Context Request req,
            @Context UriInfo uriInfo,
            @PathParam("collection") String collectionId)
            throws StorageException {
        super(req, uriInfo, collectionId);
        ResponseUtils.ensureExists(_collInfo);
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.impl.jaxrs.AbstractBaseResource#lastModification()
     */
    @Override
    protected long lastModification() {
        return _collInfo.getUpdated();
    }

    /**
     * Generates the collection feed.
     *
     * @param headers The HTTP headers.
     * @return A reponse containing the collection feed.
     * @throws StorageException In case of an error.
     * @throws UnsupportedMediaTypeException 
     */
    @GET
    public Response getCollection(@Context HttpHeaders headers) throws StorageException, UnsupportedMediaTypeException {
        final ResponseBuilder builder = makeResponseBuilder();
        final IWritableRepresentation writable = _feedFactory.createCollectionFeed(
                getBaseURI(), getStorage(), getConfiguration(), 
                _collInfo, 
                MediaTypeUtils.toAtomicoMediaType(headers.getAcceptableMediaTypes()));
        return ResponseUtils.buildStreamingEntity(builder, writable);
    }

    /**
     * Delegates the request to the "fragements" subresource.
     *
     * @return The fragemnts feed.
     * @throws StorageException In case of an error.
     */
    @Path("fragments/")
    public FragmentsFeedResource getFragments() throws StorageException {
        return new FragmentsFeedResource(_request, _uriInfo, _collInfo);
    }

    /**
     * Delegates the request to the "snapshots" subresource.
     *
     * @return The snapshots feed.
     * @throws StorageException In case of an error.
     */
    @Path("snapshots/")
    public SnapshotsFeedResource getSnapshots() throws StorageException {
        return new SnapshotsFeedResource(_request, _uriInfo, _collInfo);
    }

}
